package smodul

import "github.com/jinzhu/gorm"



type Record struct {
	gorm.Model
	RobotsId uint `gorm:"not null"`
	RobotName string `gorm:"type:varchar(255);not null"`
	Customer string `gorm:"type:varchar(255)"`
	Event string `gorm:"type:varchar(255)"`
}

func NewRecord(rid uint,robotNmae,customer,eve string)*Record{
	return &Record{
		RobotsId:rid,
		Model:     gorm.Model{},
		RobotName: robotNmae,
		Customer:   customer,
		Event:     eve,
	}
}
func NewRecordAndInsert(rid uint,robotNmae,customer,eve string)error{
	record:= &Record{
		RobotsId:rid,
		Model:     gorm.Model{},
		RobotName: robotNmae,
		Customer:   customer,
		Event:     eve,
	}
	return record.Insert()
}

func (r * Record) Insert()error{
	err:=MysqlDB.Create(r).Error
	if err!=nil{
		return err
	}
	return nil
}

func Get_All_Record_Name(name string,limit int,offset int)([]Record,error){
	list := make([]Record,0)
	err:=MysqlDB.Where("robot_name = ?",name).Order("id desc").Offset(offset).Limit(limit).Find(&list).Error
	if err!=nil{
		return nil,err
	}
	return list,nil
}

//Get_All_Record 获取指定数量的日志
func Get_All_Record(limit int,offset int)([]Record,error){
	list := make([]Record,0)
	err:=MysqlDB.Order("id desc").Offset(offset).Limit(limit).Find(&list).Error
	if err!=nil{
		return nil,err
	}
	return list,nil
}
